#!/bin/bash

# Eagle Project - Linux Production Deployment Script
# 使用方法: ./scripts/deploy-linux.sh

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 配置变量
PROJECT_NAME="eagle"
SERVICE_NAME="eagle-service"
DOCKER_IMAGE="eagle-service:latest"
CONFIG_FILE="config/config.prod.yaml"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Eagle Project - Linux Production Deployment${NC}"
echo -e "${BLUE}========================================${NC}"
echo

# 检查是否为 root 用户
if [[ $EUID -eq 0 ]]; then
   echo -e "${YELLOW}警告: 不建议使用 root 用户运行此脚本${NC}"
   read -p "是否继续? (y/N): " -n 1 -r
   echo
   if [[ ! $REPLY =~ ^[Yy]$ ]]; then
       exit 1
   fi
fi

# 检查 Docker
echo -e "${YELLOW}[1/6] 检查 Docker 环境...${NC}"
if ! command -v docker &> /dev/null; then
    echo -e "${RED}错误: Docker 未安装${NC}"
    echo "请先安装 Docker: https://docs.docker.com/engine/install/"
    exit 1
fi

if ! docker info &> /dev/null; then
    echo -e "${RED}错误: Docker 服务未启动${NC}"
    echo "请启动 Docker 服务: sudo systemctl start docker"
    exit 1
fi

echo -e "${GREEN}✓ Docker 环境正常${NC}"

# 检查 Docker Compose
echo -e "${YELLOW}[2/6] 检查 Docker Compose...${NC}"
if ! command -v docker-compose &> /dev/null; then
    echo -e "${RED}错误: Docker Compose 未安装${NC}"
    echo "请先安装 Docker Compose: https://docs.docker.com/compose/install/"
    exit 1
fi

echo -e "${GREEN}✓ Docker Compose 已安装${NC}"

# 检查配置文件
echo -e "${YELLOW}[3/6] 检查配置文件...${NC}"
if [[ ! -f "$CONFIG_FILE" ]]; then
    echo -e "${RED}错误: 配置文件 $CONFIG_FILE 不存在${NC}"
    echo "请先创建生产环境配置文件"
    exit 1
fi

echo -e "${GREEN}✓ 配置文件存在${NC}"

# 创建必要的目录
echo -e "${YELLOW}[4/6] 创建必要目录...${NC}"
sudo mkdir -p /opt/$PROJECT_NAME/{logs,data,config}
sudo mkdir -p /var/log/$PROJECT_NAME
sudo chown -R $USER:$USER /opt/$PROJECT_NAME
sudo chown -R $USER:$USER /var/log/$PROJECT_NAME

echo -e "${GREEN}✓ 目录创建完成${NC}"

# 复制配置文件
echo -e "${YELLOW}[5/6] 复制配置文件...${NC}"
cp $CONFIG_FILE /opt/$PROJECT_NAME/config/
cp deploy/docker-compose/docker-compose.prod.yaml /opt/$PROJECT_NAME/

# 创建环境变量文件
cat > /opt/$PROJECT_NAME/.env << EOF
# 生产环境配置
MYSQL_ROOT_PASSWORD=$(openssl rand -base64 32)
MYSQL_PASSWORD=$(openssl rand -base64 32)
REDIS_PASSWORD=$(openssl rand -base64 32)
JWT_SECRET=$(openssl rand -base64 64)
EOF

echo -e "${GREEN}✓ 配置文件复制完成${NC}"

# 构建和启动服务
echo -e "${YELLOW}[6/6] 构建和启动服务...${NC}"

# 检查是否有预构建的二进制文件
if [[ -f "bin/eagle-service" ]]; then
    echo "使用预构建的二进制文件"
    cp bin/eagle-service /opt/$PROJECT_NAME/
else
    echo "构建 Docker 镜像..."
    docker build -t $DOCKER_IMAGE -f deploy/docker/Dockerfile .
fi

# 启动服务
cd /opt/$PROJECT_NAME
docker-compose -f docker-compose.prod.yaml up -d

echo -e "${GREEN}✓ 服务启动完成${NC}"

# 等待服务启动
echo "等待服务启动..."
sleep 10

# 检查服务状态
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}部署完成!${NC}"
echo -e "${BLUE}========================================${NC}"
echo
echo -e "${YELLOW}服务信息:${NC}"
echo -e "  - 应用服务: ${BLUE}http://localhost:8080${NC}"
echo -e "  - 健康检查: ${BLUE}http://localhost:8080/health${NC}"
echo -e "  - Nginx: ${BLUE}http://localhost:80${NC}"
echo
echo -e "${YELLOW}管理命令:${NC}"
echo -e "  查看服务状态: ${BLUE}docker-compose -f /opt/$PROJECT_NAME/docker-compose.prod.yaml ps${NC}"
echo -e "  查看日志: ${BLUE}docker-compose -f /opt/$PROJECT_NAME/docker-compose.prod.yaml logs -f${NC}"
echo -e "  停止服务: ${BLUE}docker-compose -f /opt/$PROJECT_NAME/docker-compose.prod.yaml down${NC}"
echo -e "  重启服务: ${BLUE}docker-compose -f /opt/$PROJECT_NAME/docker-compose.prod.yaml restart${NC}"
echo
echo -e "${YELLOW}配置文件位置:${NC}"
echo -e "  - 应用配置: ${BLUE}/opt/$PROJECT_NAME/config/${NC}"
echo -e "  - 环境变量: ${BLUE}/opt/$PROJECT_NAME/.env${NC}"
echo -e "  - 日志文件: ${BLUE}/var/log/$PROJECT_NAME/${NC}"
echo

# 检查服务健康状态
echo -e "${YELLOW}健康检查:${NC}"
if curl -f http://localhost:8080/health &> /dev/null; then
    echo -e "${GREEN}✓ 应用服务正常${NC}"
else
    echo -e "${RED}✗ 应用服务异常${NC}"
fi

if curl -f http://localhost/health &> /dev/null; then
    echo -e "${GREEN}✓ Nginx 服务正常${NC}"
else
    echo -e "${RED}✗ Nginx 服务异常${NC}"
fi 